﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Npgsql;


namespace Emploees
{
    public partial class PasswordForm : Form
    {
        public PasswordForm()
        {
            InitializeComponent();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtBoxPassword.UseSystemPasswordChar=true;
            this.ControlBox = true;
            this.MinimizeBox = true;
            cmbBox2.Items.Add("Пользователь");
            cmbBox2.Items.Add("Начальник");
            cmbBox2.Text = "Пользователь";
            txtBoxUserName.Select();
            
        }
        /*
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing &&
                e.CloseReason == CloseReason.MdiFormClosing &&
                e.CloseReason == CloseReason.TaskManagerClosing &&
                e.CloseReason == CloseReason.FormOwnerClosing)
            {
                e.Cancel = true;
            }
        }
        */
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Global_variables.connecttodb().Close();
            Environment.Exit(0);
        }
        private void BtnExit_Click(object sender, EventArgs e)
        {
            Global_variables.connecttodb().Close();
            //Application.Exit();
            Environment.Exit(0);
        }

        private void BtnEnter_Click(object sender, EventArgs e)
        {
            if (txtBoxUserName.Text == "robin" && txtBoxPassword.Text=="robin")
            {
                label3.ForeColor = Color.Red;
                label3.Visible = true;
                txtBoxUserName.Text = "";
                txtBoxPassword.Text = "";
            }
            label3.Visible = false;
            NpgsqlConnection conn = Global_variables.connecttodb();
            conn.Open();
            NpgsqlTransaction t = conn.BeginTransaction();
            NpgsqlCommand command = new NpgsqlCommand("\"WorkTime\".check_passwd", conn);
            command.Parameters.Add(new NpgsqlParameter("@username", NpgsqlTypes.NpgsqlDbType.Varchar));
            command.Parameters.Add(new NpgsqlParameter("@passwd", NpgsqlTypes.NpgsqlDbType.Text));
            command.Parameters[0].Value = txtBoxUserName.Text;
            command.Parameters[1].Value = txtBoxPassword.Text;
            command.CommandType = CommandType.StoredProcedure;

            NpgsqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                Global_variables.id_faculty = Convert.ToInt16(dr.GetValue(3));
                Global_variables.id_department = Convert.ToInt16(dr.GetValue(4));
                Global_variables.id_user = Convert.ToInt16(dr.GetValue(5));
                Global_variables.CurrentDate();
                switch (cmbBox2.Text)
                {

                    case "Начальник": if (String.Equals(Convert.ToString(dr.GetValue(2)), "chief"))
                        {
                            MessageBox.Show("Начальник");
                            //Form.ActiveForm.Hide();
                            Program.passwordform.Hide();
                            conn.Close();
                            dr.Close();
                            Program.worktimeform = new WorkTimeForm();
                            Program.worktimeform.ShowDialog();
                            Program.flag = true;
                            
                            
                        }
                        else
                        {
                            label3.Text = "Вы не являетесь начальником";
                            label3.TextAlign = ContentAlignment.MiddleCenter;
                            label3.ForeColor = Color.Red;
                            label3.Visible = true;
                        }
                        break;
                    default:
                        {
                            MessageBox.Show("Подчиненный");
                            Program.flag = true;
                            Program.passwordform.Hide();
                            conn.Close();
                            dr.Close();
                            Program.timemanage = new Time_manage();
                            Program.timemanage.ShowDialog();
                        }
                        break;
                }

            }
            else
            {
                label3.Text = "Имя пользователя или пароль неверно";
                label3.TextAlign = ContentAlignment.MiddleCenter;
                label3.ForeColor = Color.Red;
                label3.Visible = true;
                txtBoxUserName.Text = "";
                txtBoxPassword.Text = "";
            }
            /*
            while (dr.Read())
            {

                Console.WriteLine(dr.GetValue(0) + "\t" + dr.GetValue(1) + "\t" + dr.GetValue(2));
                
            }
             */
            /*
            dr.NextResult();

            while (dr.Read())
            {

                Console.WriteLine(dr.GetValue(0));
            }
 */
            //dr.Close();
            //conn.Close();
        }

       
           
      
    }
}
